package com.dlc.shop.listener;

import com.dlc.shop.bean.enums.OrderType;
import com.dlc.shop.bean.event.OrderSettlementEvent;
import com.dlc.shop.bean.event.RefundSuccessSettlementEvent;
import com.dlc.shop.bean.order.ConfirmOrderOrder;
import com.dlc.shop.service.ShopWalletService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * 确认收货的事件
 * @author yami
 */
@Slf4j
@Component("orderSettlementShopListener")
@AllArgsConstructor
public class OrderSettlementListener {

    private final ShopWalletService shopWalletService;

    /**
     * 订单确认收货后过了维权期的订单结算操作
     * @param event
     */
    @EventListener(OrderSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void defaultSettlementOrderEvent(OrderSettlementEvent event) {
        for (com.dlc.shop.bean.model.Order order : event.getOrder()) {
            //如果是积分订单无需操作
            if(Objects.equals(order.getOrderType() , OrderType.SCORE.value())){
                continue;
            }
            shopWalletService.addAmountBySettledOrder(order.getOrderNumber());
        }

    }

    /**
     * 部分退款且订单关闭的情况下的订单结算操作
     * @param event
     */
    @EventListener(RefundSuccessSettlementEvent.class)
    @Order(ConfirmOrderOrder.DEFAULT)
    public void refundSuccessSettlementOrderEvent(RefundSuccessSettlementEvent event) {
        shopWalletService.settlementShopAmountByRefund(event.getOrderChangeShopWalletAmountBO(),event.getOrder());

    }

}
